package com.factory.rental.controller;

import com.factory.rental.common.Result;
import com.factory.rental.dto.ReportDTO;
import com.factory.rental.dto.ReportStatisticsDTO;
import com.factory.rental.model.Report;
import com.factory.rental.service.ReportService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import com.factory.rental.common.PageResult;
import com.factory.rental.common.PageSupport;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * 报表控制器
 */
@RestController
@RequestMapping("/reports")
@Api(tags = "报表管理")
public class ReportController {

    private final ReportService reportService;

    @Autowired
    public ReportController(ReportService reportService) {
        this.reportService = reportService;
    }

    @ApiOperation("生成报表")
    @PostMapping("save")
    public Result<Report> generateReport(@RequestBody ReportDTO reportDTO) {
        Report report = reportService.generateReport(reportDTO);
        return Result.success(report, "报表生成成功");
    }

    @ApiOperation("获取报表详情")
    @GetMapping("/{id}")
    public Result<Report> getReportById(@PathVariable Long id) {
        Optional<Report> report = reportService.getReportById(id);
        if (report.isPresent()) {
            return Result.success(report.get());
        } else {
            return Result.error(404, "未找到指定ID的报表");
        }
    }

    @ApiOperation("根据类型获取报表列表")
    @GetMapping("/type/{type}")
    public Result<List<Report>> getReportsByType(@PathVariable Report.ReportType type) {
        List<Report> reports = reportService.getReportsByType(type);
        return Result.success(reports);
    }

    @ApiOperation("获取所有报表（分页）")
    @GetMapping("pageList")
    public PageResult<Report> getAllReports(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {
        Pageable pageable = PageSupport.ofPage(page, size);
        Page<Report> reports = reportService.getAllReports(pageable);
        return PageResult.success(reports);
    }

    @ApiOperation("更新报表")
    @PutMapping("/{id}")
    public Result<Report> updateReport(@PathVariable Long id, @RequestBody ReportDTO reportDTO) {
        try {
            Report updatedReport = reportService.updateReport(id, reportDTO);
            return Result.success(updatedReport, "报表更新成功");
        } catch (RuntimeException e) {
            return Result.error(404, "未找到指定ID的报表");
        }
    }

    @ApiOperation("删除报表")
    @DeleteMapping("/{id}")
    public Result<Void> deleteReport(@PathVariable Long id) {
        try {
            reportService.deleteReport(id);
            return Result.success(null, "报表删除成功");
        } catch (RuntimeException e) {
            return Result.error(404, "未找到指定ID的报表");
        }
    }

    @ApiOperation("导出报表")
    @GetMapping("/{id}/export")
    public Result<String> exportReport(@PathVariable Long id) {
        try {
            String filePath = reportService.exportReport(id);
            return Result.success(filePath, "报表导出成功");
        } catch (RuntimeException e) {
            return Result.error(500, "导出报表失败: " + e.getMessage());
        }
    }

    @ApiOperation("获取报表统计数据")
    @GetMapping("/statistics")
    public Result<ReportStatisticsDTO> getReportStatistics() {
        ReportStatisticsDTO statistics = reportService.getReportStatistics();
        return Result.success(statistics);
    }
}